﻿using System.Linq;
using K9Nano.Share.Exceptions;
using Microsoft.AspNetCore.Identity;

namespace K9Nano.Application.Extensions
{
    public static class IdentityResultExtensions
    {
        public static K9UserFriendlyException ToException(this IdentityResult result)
        {
            var errors = string.Join(",", result.Errors.Select(x => x.Description));
            return new K9UserFriendlyException(errors);
        }

        public static void EnsureSuccess(this IdentityResult result)
        {
            if (!result.Succeeded)
            {
                var errors = string.Join(",", result.Errors.Select(x => x.Description));
                throw new K9UserFriendlyException(errors);
            }
        }
    }
}